package com.zf.yichat.utils.XSpay.TransForm;


import com.alibaba.fastjson.JSONObject;
import com.zf.yichat.utils.common.DateUtils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 交易报文
 * 交易报文是面向商户的，各交易报文中的字段必须与商户提交请求中的参数key一致
 * <p>Title: BaseTransForm</p>
 * <p>Description: </p>
 * <p>Company: </p>
 */
public abstract class BaseTransForm implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String VERSION = "version";
    private static final String TRAN_CODE = "tranCode";
    private static final String MER_ID = "merId";
    private static final String MER_ORDER_ID = "merOrderId";
    private static final String CHARSET = "charset";
    private static final String SIGN_TYPE = "signType";
    private static final String SIGN_VALUE = "signValue";
    private static final String MER_ATTACH = "merAttach";
    private static final String SUBMIT_TIME = "submitTime";
    private static final String MSG_CIPHER_TEXT = "msgCiphertext";
    private static final String RESULT_CODE = "resultCode";
    private static final String ERROR_CODE = "errorCode";
    private static final String ERROR_MSG = "errorMsg";

    // 暂定公共的报文字段
    private String tranCode;
    private String version;
    private String merId;
    private String merOrderId;
    private String signType;
    private String signValue;
    private String msgCiphertext;
    private String charset;
    private String merAttach;
    private String submitTime;

    //提交地址
    private String submitUrl;

    public String getTranCode() {
        return tranCode;
    }

    public void setTranCode(String tranCode) {
        this.tranCode = tranCode;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getMerId() {
        return merId;
    }

    public void setMerId(String merId) {
        this.merId = merId;
    }

    public String getMerOrderId() {
        return merOrderId;
    }

    public void setMerOrderId(String merOrderId) {
        this.merOrderId = merOrderId;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getSignValue() {
        return signValue;
    }

    public void setSignValue(String signValue) {
        this.signValue = signValue;
    }

    public String getMsgCiphertext() {
        return msgCiphertext;
    }

    public void setMsgCiphertext(String msgCiphertext) {
        this.msgCiphertext = msgCiphertext;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getMerAttach() {
        return merAttach;
    }

    public void setMerAttach(String merAttach) {
        this.merAttach = merAttach;
    }

    public String getSubmitTime() {
        return submitTime;
    }

    public void setSubmitTime(String submitTime) {
        this.submitTime = submitTime;
    }

    public String getSubmitUrl() {
        return submitUrl;
    }

    public void setSubmitUrl(String submitUrl) {
        this.submitUrl = submitUrl;
    }

    /**
     * 获取公共返回参数<br>
     * Map中的key必须与返回XML文件中的field的name值一致<br>
     * 子类可选择性重写
     *
     * @return
     */
    @SuppressWarnings("static-access")
    public Map<String, Object> getCommonRespParams() {
        Map<String, Object> commonParams = new HashMap<String, Object>();
        commonParams.put(this.VERSION, this.version);
        commonParams.put(this.TRAN_CODE, this.tranCode);
        commonParams.put(this.MER_ORDER_ID, this.merOrderId);
        commonParams.put(this.MER_ID, this.merId);
        commonParams.put(this.MER_ATTACH, this.merAttach);
        commonParams.put(this.CHARSET, this.charset);
        commonParams.put(this.SIGN_TYPE, this.signType);
        return commonParams;
    }

    /**
     * 获取共用请求的签名字段
     *
     * @return Map<String, Object>
     * @author wgy
     * @date 2018年8月10日 上午10:26:54
     * @description 一句话描述功能
     */
    @SuppressWarnings("static-access")
    public Map<String, Object> getCommonReqSignParams() {
        Map<String, Object> commonParams = new HashMap<String, Object>();
        commonParams.put(this.TRAN_CODE, this.tranCode);
        commonParams.put(this.VERSION, this.version);
        commonParams.put(this.MER_ID, this.merId);
        commonParams.put(this.MER_ORDER_ID, this.merOrderId);
        commonParams.put(this.SUBMIT_TIME, this.submitTime);
        commonParams.put(this.MSG_CIPHER_TEXT, this.msgCiphertext);
        commonParams.put(this.SIGN_TYPE, this.signType);
        return commonParams;
    }

    /**
     * 获取加密json串明文
     *
     * @return String
     * @author wgy
     * @date 2018年8月27日 下午6:08:46
     * @description 一句话描述功能
     */
    public abstract String getEncryptJsonStr();

    /**
     * 获取验签字段
     *
     * @return
     */
    public abstract String getVerifyJsonStr();

    /**
     * 获取提交字段
     *
     * @return
     */
    public abstract String getSubmitJsonStr();


    public void initCommonParams(String tranCode, String merId, String oldMerOrderId) {
        this.version = "1.0";
        this.tranCode = tranCode;
        this.merId = merId;
        if (Objects.nonNull(oldMerOrderId)) {
            this.merOrderId = oldMerOrderId;
        } else {
            this.merOrderId = tranCode + "_" + DateUtils.formatDate(new Date(), "yyyyMMddHHmmss");
        }
        this.submitTime = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss");
        this.signType = "1";
        this.merAttach = "";
        this.charset = "UTF-8";
    }

    public String getCommonSignStr() {
        StringBuffer sb = new StringBuffer();
        sb.append(this.VERSION);
        sb.append("=[");
        sb.append(getVersion());
        sb.append("]");

        sb.append(this.TRAN_CODE);
        sb.append("=[");
        sb.append(getTranCode());
        sb.append("]");

        sb.append(this.MER_ID);
        sb.append("=[");
        sb.append(getMerId());
        sb.append("]");

        sb.append(this.MER_ORDER_ID);
        sb.append("=[");
        sb.append(getMerOrderId());
        sb.append("]");

        sb.append(this.SUBMIT_TIME);
        sb.append("=[");
        sb.append(getSubmitTime());
        sb.append("]");

        sb.append(this.MSG_CIPHER_TEXT);
        sb.append("=[");
        sb.append(getMsgCiphertext());
        sb.append("]");

        sb.append(this.SIGN_TYPE);
        sb.append("=[");
        sb.append(getSignType());
        sb.append("]");

        return sb.toString();
    }

    public String getValueByReflet(Object model, String paraName) {
        // 返回值
        String value = "";
        try {
            // 获取属性值
            Field[] fields = model.getClass().getDeclaredFields();

            for (Field field : fields) {
                field.setAccessible(true);

                if (field.getName().equals(paraName)) {
                    value = (String) field.get(model);

                    break;
                }
            }
        } catch (Exception e) {

        }
        return value;
    }

    public String getJsonStr(Object model, String[] strArr) {
        JSONObject json = new JSONObject(true);
        for (String file : strArr) {
            json.put(file, getValueByReflet(model, file));
        }
        return json.toJSONString();
    }

}
